<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="userQuery.name" placeholder="用户名/昵称/真实姓名"/>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getUserList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <div>
      <el-button v-if="hasPerm('user.add')" type="success" size="mini" @click="addUser()">添加用户</el-button>
      <el-button v-if="hasPerm('user.remove')" type="danger" size="mini" @click="removeRows()">批量删除</el-button>

    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange">

      <el-table-column
        type="selection"
        width="55"/>

      <el-table-column
        prop="username"
        label="用户名"/>

      <el-table-column
        prop="nickName"
        label="昵称"/>

      <el-table-column
        prop="name"
        label="姓名"/>

      <el-table-column
        :formatter="handGender"
        prop="gender"
        label="性别"/>

      <el-table-column
        prop="phone"
        label="手机号"/>

      <el-table-column
        prop="email"
        label="邮箱"/>

      <el-table-column label="操作" width="230" align="center">
        <template slot-scope="scope">
          <router-link :to="'/acl/user/role/'+scope.row.id">
            <el-button v-if="hasPerm('user.assgin')" type="info" size="mini" icon="el-icon-info"/>
          </router-link>
          <router-link :to="'/acl/user/update/'+scope.row.id">
            <el-button v-if="hasPerm('user.update')" type="primary" size="mini" icon="el-icon-edit"/>
          </router-link>
          <el-button
            v-if="hasPerm('user.remove')"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)"/>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="page"
      :page-sizes="[5, 10, 15, 30, 50, 100]"
      :page-size="pageSize"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @size-change="changSize"
      @current-change="changePage"/>
  </div>
</template>

<script>
import userApi from '@/api/acl/user'

export default {
  data() {
    return {
      listLoading: true,
      list: null,
      page: 1,
      total: 0,
      pageSize: 10,
      userQuery: {
        page: 1,
        limit: 10,
        name: null
      },
      isDisabled: true,
      multipleSelection: []
    }
  },

  created() {
    this.getUserList()
  },

  methods: {

    addUser() {
      this.$router.push({ path: '/acl/user/add' })
    },

    getUserList() {
      this.listLoading = true
      this.userQuery.page = this.page
      this.userQuery.limit = this.pageSize
      userApi.getPageList(this.userQuery).then(res => {
        this.listLoading = false
        this.total = Number.parseInt(res.result.counts)
        this.list = res.result.items
      }).catch(error => {
        console.log(error)
      })
    },

    handGender(user) {
      if (user.gender === '0') {
        return '女'
      }
      return '男'
    },

    changSize(pageSize) {
      this.pageSize = pageSize
      this.page = 1
      this.getUserList()
    },

    changePage(page) {
      this.page = page
      this.getUserList()
    },

    resetData() {
      this.userQuery = {}
      this.getUserList()
    },

    removeDataById(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return userApi.removeById(id)
      }).then((response) => {
        this.getUserList()
        if (response.success) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    handleSelectionChange(val) {
      this.multipleSelection = val.map(role => role.id)
      this.isDisabled = this.multipleSelection.length === 0
    },

    removeRows() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的记录!'
        })
        return
      }

      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var idList = []
        this.multipleSelection.forEach(item => {
          idList.push(item)
        })
        return userApi.removeRows(idList)
      }).then((response) => {
        this.getUserList()
        if (response.success) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

